﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using log4net;

namespace Estimate.Common.DataFileFinder
{
    /// <summary>
    /// 城镇预报文件发现类
    /// 城镇报文件中，新文件往往会发生站点信息后缺失的情况
    /// 所以要修改文件导入的接口
    /// </summary>
    public class NormalCityForeFinder:IDataFileFinder
    {
        public static readonly ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string filterFiles(string dirPath, string[] filepattern)
        {
            string[] fileArray = getFileList(dirPath, filepattern);

            if (fileArray.Length > 0)
                return fileArray[fileArray.Length - 1];
            else
                return "";
        }

        public string selectLatestFile(string f1, string f2)
        {
            if (f2 != "")
                return f2;
            else
                return f1;
        }

        public string[] GetFileFromDirectory(DateTime dt, string dirPath)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");
            List<string> result = new List<string>();

            //早晨05点预报
            string f = filterFiles(dirPath, new string[] 
                {
                    "Z_SEVP_C_BENJ_" + date0 + "20????" + "_P_RFFC-SPCC-" + date + "0000-07212.TXT"
                });
            if (f != "")
                result.Add(f);

            //早晨06点预报
            f = filterFiles(dirPath, new string[] 
                {
                    "Z_SEVP_C_BENJ_" + date0 + "21????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    "Z_SEVP_C_BENJ_" + date0 + "22????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT"
                });
            if (f != "")
                result.Add(f);

            //早晨10点预报
            f = filterFiles(dirPath, new string[] 
                {
                   "Z_SEVP_C_BENJ_" + date + "01????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                   "Z_SEVP_C_BENJ_" + date + "02????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT"
                });
            if (f != "")
                result.Add(f);
                                    

            //下午16点预报
            f = filterFiles(dirPath, new string[] 
                {
                   "Z_SEVP_C_BENJ_" + date + "07????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT",
                   "Z_SEVP_C_BENJ_" + date + "08????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT"
                });
            if (f != "")
                result.Add(f);
            return result.ToArray();
        }


        public string[] GetFileNames(DateTime dt)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");

            return new string[] 
                {
                     //早晨05点预报
                    "Z_SEVP_C_BENJ_" + date0 + "20????" + "_P_RFFC-SPCC-" + date + "0000-07212.TXT",
                    //早晨06点预报
                    "Z_SEVP_C_BENJ_" + date0 + "21????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    "Z_SEVP_C_BENJ_" + date0 + "22????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    //早晨10点预报
                    "Z_SEVP_C_BENJ_" + date + "01????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    "Z_SEVP_C_BENJ_" + date + "02????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    //下午16点预报
                     "Z_SEVP_C_BENJ_" + date + "07????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT",
                    "Z_SEVP_C_BENJ_" + date + "08????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT"
                };
        }
    

        public Dictionary<string,string[]>  GetFileListFromDirectory(DateTime dt, string dirPath)
        {
            string date = dt.ToString("yyyyMMdd");
            string date0 = dt.AddDays(-1).ToString("yyyyMMdd");
            Dictionary<string, string[]> result = new Dictionary<string, string[]>();

            //早晨05点预报
            string[] f = getFileList(dirPath, new string[] 
                {
                    "Z_SEVP_C_BENJ_" + date0 + "20????" + "_P_RFFC-SPCC-" + date + "0000-07212.TXT"
                });
            result.Add("07212",f);

            //早晨06点预报
            f = getFileList(dirPath, new string[] 
                {
                    "Z_SEVP_C_BENJ_" + date0 + "21????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                    "Z_SEVP_C_BENJ_" + date0 + "22????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT"
                });
            result.Add("16812_06",f);

            //早晨10点预报
            f = getFileList(dirPath, new string[] 
                {
                   "Z_SEVP_C_BENJ_" + date + "01????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT",
                   "Z_SEVP_C_BENJ_" + date + "02????" + "_P_RFFC-SPCC-" + date + "0000-16812.TXT"
                });
            result.Add("16812_10",f);

            //下午16点预报
            f = getFileList(dirPath, new string[] 
                {
                   "Z_SEVP_C_BENJ_" + date + "07????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT",
                   "Z_SEVP_C_BENJ_" + date + "08????" + "_P_RFFC-SPCC-" + date + "1200-16812.TXT"
                });
            result.Add("16812_16",f);

            return result;
        }

        public string[] getFileList(string dirPath, string[] filepattern)
        {
            List<string> files = new List<string>();
            foreach (string pattern in filepattern)
            {
                if (Directory.Exists(dirPath))
                {
                    files.AddRange(Directory.GetFiles(dirPath, pattern));
                }
                else
                {
                    Logger.Error(string.Format("无法找到文件夹 {0}!", dirPath));
                }
            }
            if (files.Count == 0)
                return new string[] { };

            string[] fileArray = files.ToArray();
            //对文件组排序
            for (int i = 0; i < fileArray.Length; i++)
            {
                for (int j = i + 1; j < fileArray.Length; j++)
                {
                    if (files[i].CompareTo(files[j]) > 0)
                    {
                        string temp = files[i];
                        files[i] = files[j];
                        files[j] = temp;
                    }
                }
            }

            return fileArray;
        }
}
}
